package com.tianhua.javs.core.func.translate.condition;

import com.tianhua.javs.client.bean.JavsSegmentBean;
import com.tianhua.javs.client.func.ITranslatorFunc;
import com.tianhua.javs.core.annotations.Segment;
import com.tianhua.javs.core.constant.TagConstants;
import org.springframework.stereotype.Component;


/**
 * Description:
 * date: 2022/2/12
 *
 * @author shenshuai
 * @version 1.0.0
 * @since JDK 1.8
 */
@Component(value = "ifeq")
@Segment(tag = "ifeq",scriptFuncBean = "conditionFunc")
public class Ifeq implements ITranslatorFunc {
    @Override
    public JavsSegmentBean translate(String shellSegment) {
        if(shellSegment.startsWith("ifeq(")){
            int firstIndex = shellSegment.indexOf("(");
            int lastIndex = shellSegment.lastIndexOf(")");
            String paramStr = shellSegment.substring(firstIndex+1,lastIndex);
            String [] paramArr = paramStr.split(",");
            StringBuilder builder = new StringBuilder();
            builder.append("Objects.equals(");
            builder.append(paramArr[0]);
            builder.append(TagConstants.COMMA+TagConstants.BLANK_ONE);
            builder.append(paramArr[1]);
            builder.append(TagConstants.BRACKET_RIGHT);

            String javaSegment = shellSegment.replaceFirst(paramStr,builder.toString());
            JavsSegmentBean javsSegmentBean = JavsSegmentBean.getMatchInstance(javaSegment);
            javsSegmentBean.addPackageName("java.util.Objects");
        }
        return JavsSegmentBean.getInstance(shellSegment);
    }
}
